# coding:utf8

import pymysql.cursors


def conn_to_mysql():
    conn = pymysql.connect(host='localhost',
                           user='root',
                           password='root',
                           db='omdb2',
                           charset='utf8mb4',
                           cursorclass=pymysql.cursors.DictCursor)
    try:
        with conn.cursor() as cursor:
            # Create a new record
            sql = "INSERT INTO `t_names` (`name`) VALUES (%s)"
            cursor.execute(sql, ('wxg',))
        # conn is not autocommit by default. So you must commit to save
        # your changes.
        conn.commit()

        with conn.cursor() as cursor:
            # Read a single record
            sql = "SELECT `id`, `name` FROM `t_names` WHERE `name`=%s"
            cursor.execute(sql, ('wxg',))
            result = cursor.fetchone()
            print(result)
    finally:
        conn.close()


if __name__ == '__main__':
    # print('hello world!')
    conn_to_mysql()



